package org.example.threadlocaltest.demos.configure;

import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DruidConfigure {//配置druid监控页面  , 一定要在启动类上加上@ServletComponentScan 不然不会生效

    @Value("${druid.username}")
    private String username;
    @Value("${druid.password}")
    private String password;

    @Bean
    public ServletRegistrationBean statViewServlet() {
        StatViewServlet statViewServlet = new StatViewServlet();
//向容器中注入 StatViewServlet，并将其路径映射设置为 /druid/*  ，这样就可以通过 http://localhost:8080/druid/index.html 访问到监控页面
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(statViewServlet, "/druid/*");
        //配置监控页面访问的账号和密码（选配）
        servletRegistrationBean.addInitParameter("loginUsername", username);
        servletRegistrationBean.addInitParameter("loginPassword", password);
        return servletRegistrationBean;
    }


}
